<?php


namespace seecms\lib\builder\control;

class SwitchControl extends Control
{
    protected $type = 'switch';

    protected $options = [
        0 => '开启',
        1 => '关闭'
    ];

    public function render(): string
    {
        $required = $this->required ? '<span class="layui-font-red">*</span>' : '';
        $readonly = $this->readonly ? 'readonly' : '';
        $disabled = $this->disabled ? 'disabled' : '';
        $description = $this->getDescription();

        $options = implode(',', $this->options);

        $checked = '';
        if ($this->value) {
            $checked = 'checked';
        }

        $display = 'block';
        $container = '<div class="layui-form-item">';
        if ($this->inline == 'inline') {
            $display = 'inline';
            $container = '<div class="layui-inline">';
        }

        return <<<EOF
$container
    <label class="layui-form-label">
        {$this->label}
        {$required}
    </label>
    <div class="layui-input-$display">
        <input type="checkbox"
               name="{$this->name}"
               data-type="switch"
               lay-skin="switch"
               value="1"
               lay-text="{$options}"
               $checked
               $readonly
               $disabled
        >
       $description
    </div>
</div>
EOF;

    }
}